package com.ruoyi.payment.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.payment.domain.PayMethod;
import com.ruoyi.payment.domain.dto.QueryPaymentDTO;
import com.ruoyi.payment.service.PayMethodService;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 支付方法控制器
 *
 * @author Administrator
 * @date 2023/03/10
 */
@RestController
@RequestMapping("/pay_method")
@RequiredArgsConstructor
public class PayMethodController extends BaseController {

    private final PayMethodService payMethodService;

    /**
     * 分页查询支付方法
     *
     * @param pageQuery       页面查询
     * @param queryPaymentDTO 查询支付dto
     * @return {@link TableDataInfo}<{@link PayMethod}>
     */
    @GetMapping("/page")
    @SaCheckPermission("payment:payMethod:query")
    public TableDataInfo<PayMethod> pagePayMethod(QueryPaymentDTO queryPaymentDTO, PageQuery pageQuery) {
        return payMethodService.pagePayMethod(queryPaymentDTO, pageQuery);
    }

    /**
     * 得到支付方法
     *
     * @param payMethodId 支付方法id
     * @return {@link R}<{@link PayMethod}>
     */
    @GetMapping("/{payMethodId}")
    @SaCheckPermission("payment:payMethod:query")
    public R<PayMethod> getPayMethod(@PathVariable("payMethodId") Long payMethodId) {
        return R.ok(payMethodService.getById(payMethodId));
    }

    /**
     * 删除
     *
     * @param payMethodIds 支付方法id
     * @return {@link R}<{@link Void}>
     */
    @DeleteMapping
    @SaCheckPermission("payment:payMethod:remove")
    public R<Void> delete(@Validated @NotEmpty(message = "请选择要删除的支付方式") @RequestBody List<Long> payMethodIds) {
        return toAjax(payMethodService.deleteByIds(payMethodIds));
    }

    /**
     * 保存
     *
     * @param payMethod 支付方法
     * @return {@link R}<{@link Void}>
     */
    @PostMapping
    @RepeatSubmit
    @SaCheckPermission("payment:payMethod:add")
    public R<Void> save(@RequestBody PayMethod payMethod) {
        return toAjax(payMethodService.addPayMethod(payMethod));
    }

    /**
     * 编辑
     *
     * @param payMethod 支付方法
     * @return {@link R}<{@link Void}>
     */
    @PutMapping
    @RepeatSubmit
    @SaCheckPermission("payment:payMethod:edit")
    public R<Void> edit(@Validated @NotNull(message = "修改支付方式数据不能为空") @RequestBody PayMethod payMethod) {
        return toAjax(payMethodService.editPayMethod(payMethod));
    }

}
